﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TuongLai_Schools
{
    public partial class frmHanhkiem : Form
    {
        public frmHanhkiem()
        {
            InitializeComponent();
        }
        cls_DM_Hanhkiem objHanhkiem = new cls_DM_Hanhkiem();
        private void frmHanhkiem_Load(object sender, EventArgs e)
        {
            GetAll_Caphoc();
        }
        private void GetAll_Caphoc()
        {
            gridLookUpEdit1.Properties.DataSource = objHanhkiem.GetAll_Caphoc();
            gridLookUpEdit1.Properties.DisplayMember = "TenCapHoc";
            gridLookUpEdit1.Properties.ValueMember = "CapHocID";
        }
        int caphocid = 0;
        private void grvCaphoc_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            try
            {
                DataRow dtr = grvCaphoc.GetDataRow(e.RowHandle);
                caphocid = (int)dtr["CapHocID"];
                GetAll_Hanhkiem(caphocid);
            }
            catch (Exception ex) { throw ex; }
        }
        private void GetAll_Hanhkiem(int ch)
        {
            gridControl1.DataSource = objHanhkiem.GetAll_Hanhkiem(ch);
        }
        private void Save_Hanhkiem()
        {
            clsDM_Hanhkiem objdmhk = new clsDM_Hanhkiem();
            objdmhk.TenHanhKiem = gridView1.GetFocusedRowCellValue("TenHanhKiem").ToString();
            objdmhk.GioiHangDuoi =(int)gridView1.GetFocusedRowCellValue("GioiHangDuoi");
            objdmhk.GioiHangTren = (int)gridView1.GetFocusedRowCellValue("GioiHangTren");
            objdmhk.GhiChu = gridView1.GetFocusedRowCellValue("GhiChu").ToString();
            objdmhk.FK_CapHocID = caphocid;
            bool blnResult = false;
            if (ucDataButton1.DataMode == DataState.Insert)
            {
                objdmhk.HanhKiemID = 1;
                objdmhk.isChangeable = false;
                blnResult = objHanhkiem.Insert(objdmhk);
            }
            else
            {
                objdmhk.HanhKiemID = (int)gridView1.GetFocusedRowCellValue("HanhKiemID");
                objdmhk.isChangeable = (bool)gridView1.GetFocusedRowCellValue("isChangeable");
                blnResult = objHanhkiem.update(objdmhk);
            }

            if (blnResult)
            {
                clsCommon.SaveSuccessfully();
                ucDataButton1.DataMode = DataState.View;
                gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                gridView1.OptionsBehavior.Editable = false;
                gridLookUpEdit1.Enabled = true;
            }
            else
            {
                clsCommon.SaveNotSuccessfully();
                return;
            }
        }

        private void ucDataButton1_SaveHandler()
        {
            Save_Hanhkiem();
        }

        private void ucDataButton1_DeleteHandler()
        {
            if (clsCommon.ConfirmDeletion(gridView1.GetFocusedRowCellValue("TenHanhKiem").ToString()) == DialogResult.No)
                return;
            objHanhkiem.delete((int)gridView1.GetFocusedRowCellValue("HanhKiemID"));
            GetAll_Hanhkiem(caphocid);
        }

        private void ucDataButton1_InsertHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
            gridView1.OptionsBehavior.Editable = true;
            gridLookUpEdit1.Enabled = false;
        }

        private void ucDataButton1_CancelHandler()
        {
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gridView1.OptionsBehavior.Editable = false;
            GetAll_Hanhkiem(caphocid);
            gridLookUpEdit1.Enabled = true;
        }

        private void ucDataButton1_EditHandler()
        {
            gridView1.OptionsBehavior.Editable = true;
            gridLookUpEdit1.Enabled = false;
        }

    }
}
